package com.lovo.hb.controller;

import com.lovo.hb.dao.IUserDao;
import com.lovo.hb.dao.IUserRoleDao;
import com.lovo.hb.dto.RoleDto;
import com.lovo.hb.entity.UserEntity;
import com.lovo.hb.vo.RoleVo;
import com.lovo.hb.vo.TeacherVo;
import com.lovo.hb.vo.UserVo;
import com.lovo.util.ConvertBeanUtil;
import com.lovo.util.ConvertCallBack;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    IUserRoleDao userRoleDao;
    @Autowired
    IUserDao userDao;
    @GetMapping("userName/{name}")
    public String userName(@PathVariable("name")String name){
        int i=1/0;
        return name;
    }
@GetMapping("userInfo")
  public ResponseEntity<UserVo> userInfo(){
        UserVo vo=new UserVo();
        vo.setUserName("赵云");
      TeacherVo tvo=null;
      List<TeacherVo> list=new ArrayList<>();
        for(int i=0;i<5;i++){
            tvo=new TeacherVo();
            tvo.setTeacherName("陈老师"+i);
            tvo.setTeacherClass("java"+i);
            list.add(tvo);
        }
        vo.setList(list);
        return ResponseEntity.ok(vo);
  }
  @GetMapping("userRole/{userName}")
  public List<RoleVo> userRole(@PathVariable("userName")String userName){
    List<RoleDto> roleDtos=  userRoleDao.findAllByUserName(userName,0);
      List<RoleVo> roleVos=new ArrayList<>();
    roleDtos.stream().forEach(roleDto -> {
        RoleVo roleVo=null;

       roleVo= ConvertBeanUtil.copy(roleDto,RoleVo.class,
                (convertBefore, convertAfter) -> {
            convertAfter.setMessage("角色信息");
        });
        roleVos.add(roleVo);
    });
  return roleVos;
    }
     @GetMapping("pages/{name}/{currentPage}/{pageSize}")
    public Page<UserEntity> pages(@PathVariable("name") String name,
                                  @PathVariable("currentPage")int currentPage,
                                  @PathVariable("pageSize")int pageSize){
         PageRequest pageRequest=PageRequest.of(currentPage-1,pageSize);
       return   userDao.findAllByLikeNameForPage(name+"%",pageRequest);
    }
}
